import json
import os.path
from typing import Any

class MyConfig(object):

    def __init__(self,fileName:str,encoding:str="UTF-8"):
        self.fileName=fileName
        self.fileEncoding=encoding
        self.config = {}        
        try:
            if not os.path.isfile(fileName): raise Exception(f"Файл {fileName} отсутсвует")
            with open(file=fileName,encoding=encoding,mode="r") as fs:
                self.config = json.load(fs)
        except: raise       

    def get(self,key:str,defaultValue)->Any:
        return self.config.get(key,defaultValue)
    
    def set(self,key:str,value):
        try:
            self.config.update({key:value})
        except: raise

    def get_keys(self)->dict:
        keys={}
        for key in self.config.keys():
            keys[key]=str(type(self.config[key]))
        return keys
    
    def sync(self):
        try:
            with open(self.fileName,encoding=self.fileEncoding,mode="w") as fs:
                json.dump(self.config, fs, indent=2, sort_keys=False, ensure_ascii=False)
                fs.flush()
        except: raise
            